package com.zhaoyang.dao;

import com.zhaoyang.model.Page;
import com.zhaoyang.model.Student;
import com.zhaoyang.util.StringUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 赵扬
 * @time 2020/4/27 11:28
 */
public class StudentDao extends BaseDao {

    /**
     * 添加学生
     *
     * @param student
     * @return
     */
    public boolean addStudent(Student student) {
        String sql = "insert into s_student values(null,?,?,sha(?),?,?,?,?,null)";
        int cnt = template.update(sql, student.getSn(), student.getName(),
                student.getPassword(), student.getClazzId(),
                student.getSex(), student.getMobile(), student.getQQ());
        return cnt == 1;
    }

    /**
     * 根据学生ID 编辑学生信息
     *
     * @param student
     * @return
     */
    public boolean editStudent(Student student) {
        String sql = "update s_student set name = ? ,sex = ? ,mobile = ?,qq = ?,clazz_id = ? where id = ?";
        int cnt = template.update(sql, student.getName(), student.getSex(), student.getMobile(),
                student.getQQ(), student.getClazzId(), student.getId());
        return cnt == 1;
    }

    /**
     * 设置学生头像
     *
     * @param student
     * @return
     */
    public boolean setStudentAvatar(Student student) {
        String sql = "update s_student set photo = ? where id = ?";
        int cnt = template.update(sql, student.getPhoto(), student.getId());
        return cnt == 1;
    }

    /**
     * 根据id数组删除学生信息
     *
     * @param ids
     * @return
     */
    public boolean deleteStudent(String[] ids) {
        final int len = ids.length;
        StringBuilder sql = new StringBuilder("delete from s_student where id in(");
        for (int i = 0; i < len; i++) {
            if (len - 1 == i) {
                sql.append("?)");
            } else {
                sql.append("?,");
            }
        }
        Object[] objects = (Object[]) ids;
        int cnt = template.update(sql.toString(), objects);
        return cnt == len;
    }

    /**
     * 根据id获取学生信息
     *
     * @param id
     * @return
     */
    public Student getStudent(int id) {
        String sql = "select * from s_student where id = ?";
        List<Student> students = template.query(sql, new BeanPropertyRowMapper<Student>(Student.class), id);

        if (0 == students.size()) {
            return null;
        }

        return students.get(0);
    }

    /**
     * 获取学生列表
     *
     * @param student
     * @param page
     * @return
     */
    public List<Student> getStudentList(Student student, Page page) {
        List<Student> ret = new ArrayList<Student>();
        String sql = "select * from s_student ";
        if (!StringUtil.isEmpty(student.getName())) {
            sql += "and name like '%" + student.getName() + "%'";
        }
        if (student.getClazzId() != 0) {
            sql += " and clazz_id = " + student.getClazzId();
        }
        if (student.getId() != 0) {
            sql += " and id = " + student.getId();
        }
        sql += " limit " + page.getStart() + "," + page.getPageSize();
        ResultSet resultSet = query(sql.replaceFirst("and", "where"));
        try {
            while (resultSet.next()) {
                Student s = new Student();
                s.setId(resultSet.getInt("id"));
                s.setClazzId(resultSet.getInt("clazz_id"));
                s.setMobile(resultSet.getString("mobile"));
                s.setName(resultSet.getString("name"));
                s.setPassword(resultSet.getString("password"));
                s.setPhoto(resultSet.getBinaryStream("photo"));
                s.setQQ(resultSet.getString("qq"));
                s.setSex(resultSet.getString("sex"));
                s.setSn(resultSet.getString("sn"));
                ret.add(s);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 获取学生总数
     *
     * @param student
     * @return
     */
    public int getStudentListTotal(Student student) {
        int total = 0;
        String sql = "select count(*)as total from s_student ";
        if (!StringUtil.isEmpty(student.getName())) {
            sql += "and name like '%" + student.getName() + "%'";
        }
        if (student.getClazzId() != 0) {
            sql += " and clazz_id = " + student.getClazzId();
        }
        if (student.getId() != 0) {
            sql += " and id = " + student.getId();
        }
        ResultSet resultSet = query(sql.replaceFirst("and", "where"));
        try {
            while (resultSet.next()) {
                total = resultSet.getInt("total");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return total;
    }

    /**
     * 修改学生密码
     *
     * @param id
     * @param newPassword
     * @return
     */
    public boolean editPassword(int id, String newPassword) {

        String sql = "update s_student set password = sha(?) where id = ?";
        int cnt = template.update(sql, newPassword, id);

        return cnt == 1;
    }

    /**
     * 学生登录
     *
     * @param name
     * @param password
     * @return
     */
    public Student login(String name, String password) {
        String sql = "select * from s_student where name = '" + name + "' and password = '" + password + "'";
        ResultSet resultSet = query(sql);
        try {
            if (resultSet.next()) {
                Student student = new Student();
                student.setId(resultSet.getInt("id"));
                student.setName(resultSet.getString("name"));
                student.setPassword(resultSet.getString("password"));
                student.setClazzId(resultSet.getInt("clazz_id"));
                student.setMobile(resultSet.getString("mobile"));
                student.setPhoto(resultSet.getBinaryStream("photo"));
                student.setQQ(resultSet.getString("qq"));
                student.setSex(resultSet.getString("sex"));
                student.setSn(resultSet.getString("sn"));
                return student;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
